package com.dongao.activity;

import java.io.Serializable;
import java.util.ArrayList;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.TextView;

import com.dongao.model.Exam;
import com.dongao.utils.FileUtil;
import com.dongao.utils.JsonUtil;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity implements OnCheckedChangeListener {
	private TabHost mTabHost;
	private Intent mOnlineCourseIntent;
	private Intent mOfflineCourseIntent;
	private Intent mCourseRecordIntent;
	private Intent mSettingIntent;
	private Intent mExitIntent;
	private TextView actionBar;
	private ArrayList<Exam> exams;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		actionBar = (TextView)findViewById(R.id.actionBar);
		
		Intent intent = getIntent();
		exams = (ArrayList<Exam>)intent.getSerializableExtra("exams");
		
		mOnlineCourseIntent = new Intent(this,OnlineCourseActivity.class).putExtra("exams", (Serializable)exams);
		mOfflineCourseIntent = new Intent(this,OfflineCourseActivity.class);
		mCourseRecordIntent = new Intent(this,CourseRecordActivity.class);
		mSettingIntent = new Intent(this,SettingsActivity.class);
		
		((RadioButton) findViewById(R.id.radio_button_online_course)).setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button_offline_course)).setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button_course_record)).setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button_setting)).setOnCheckedChangeListener(this);
		((RadioButton) findViewById(R.id.radio_button_exit)).setOnCheckedChangeListener(this);
		
		setupIntent();
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(isChecked){
			switch (buttonView.getId()) {
			case R.id.radio_button_online_course:
				this.mTabHost.setCurrentTabByTag("TAB_ONLINE_COURSE");
				actionBar.setText(R.string.course_online);
				break;
			case R.id.radio_button_offline_course:
				actionBar.setText(R.string.course_offline);
				this.mTabHost.setCurrentTabByTag("TAB_OFFLINE_COURSE");
				break;
			case R.id.radio_button_course_record:
				actionBar.setText(R.string.course_record);
				this.mTabHost.setCurrentTabByTag("TAB_COURSE_RECORD");
				break;
			case R.id.radio_button_setting:
				actionBar.setText(R.string.setting);
				this.mTabHost.setCurrentTabByTag("TAB_SETTING");
				break;
			case R.id.radio_button_exit:
				this.mTabHost.setCurrentTabByTag("TAB_EXIT"); //这里直接退出
				break;
			}
		}
	}
	
	@SuppressWarnings("unused")
	private void setupIntent() {
		this.mTabHost = getTabHost();
		TabHost localTabHost = this.mTabHost;

		localTabHost.addTab(buildTabSpec("TAB_ONLINE_COURSE", R.string.course_online,
				R.drawable.icon_1_n, this.mOnlineCourseIntent));

		localTabHost.addTab(buildTabSpec("TAB_OFFLINE_COURSE", R.string.course_offline,
				R.drawable.icon_2_n, this.mOfflineCourseIntent));

		localTabHost.addTab(buildTabSpec("TAB_COURSE_RECORD", R.string.course_record,
				R.drawable.icon_3_n, this.mCourseRecordIntent));

		localTabHost.addTab(buildTabSpec("TAB_SETTING", R.string.setting,
				R.drawable.icon_4_n, this.mSettingIntent));

		localTabHost.addTab(buildTabSpec("TAB_EXIT", R.string.exit, 
				R.drawable.icon_5_n, this.mSettingIntent)); // 这里使用了setting的

	}

	
	private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,	final Intent content) {
		return this.mTabHost.newTabSpec(tag).setIndicator(getString(resLabel),
				getResources().getDrawable(resIcon)).setContent(content);
	}

}
